package com.spica.platform.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spica.platform.base.model.PageResult;
import com.spica.platform.base.model.ResponseEntity;
import com.spica.platform.base.model.SysRole;
import com.spica.platform.user.constants.SysRoleConstant;
import com.spica.platform.user.exception.SysRoleException;
import com.spica.platform.user.exception.SysRoleRtnCodEnum;
import com.spica.platform.user.model.request.SysRoleQueryParam;
import com.spica.platform.user.service.ISysRoleService;
import com.spica.platform.user.service.ISysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.vavr.control.Try;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 */
@Slf4j
@RestController
@Api(tags = SysRoleConstant.SYS_ROLE_API)
public class SysRoleController {
	@Autowired
	private ISysRoleService sysRoleService;
	@Autowired
	private ISysUserRoleService sysUserRoleService;

	/**
	 * 后台管理查询角色
	 */
	@GetMapping("/roles/list")
	@ApiOperation(value = SysRoleConstant.FIND_ROLES_BY_PARAM)
	public PageResult<SysRole> findSysRolesByParam(Page<SysRole> page, SysRoleQueryParam sysRoleQueryParam) {
		return sysRoleService.findSysRolesByParam(page, sysRoleQueryParam);
	}

	/**
	 * 根据roleid查询用户角色
	 * @param roleId 角色id
	 * @return 用户角色
	 */
	@GetMapping(value = "/roles/roleId", params = "roleId")
	@ApiOperation(value = SysRoleConstant.FIND_ROLE_BY_ID)
	public ResponseEntity<SysRole> findRoleById(@RequestParam Long roleId) {
		return  ResponseEntity.succeed(sysRoleService.getById(roleId));
	}

	/**
	 * 用户管理查询所有角色
	 */
	@GetMapping("/allRoles")
	@ApiOperation(value = SysRoleConstant.FIND_ALL_ROLES)
	public ResponseEntity<List<SysRole>> findAll() {
		List<SysRole> result = sysRoleService.list();
		return ResponseEntity.succeed(result);
	}

	/**
	 * 角色新增或者更新
	 */
	@SneakyThrows
	@PostMapping("/roles/saveOrUpdate")
	@ApiOperation(value = SysRoleConstant.SAVE_ROLE)
	public ResponseEntity saveOrUpdate(@RequestBody SysRole sysRole) {

		Try.of(() -> sysRoleService.saveOrUpdateRole(sysRole)).onFailure(ex -> log.error("role-saveOrUpdate-error", ex))
				.getOrElseThrow(item -> new SysRoleException(SysRoleRtnCodEnum.SYS_ROLE_00001));
		return ResponseEntity.succeed(SysRoleRtnCodEnum.SYS_ROLE_00002.getRtnMessage());

	}

	/**
	 * 后台管理删除角色 delete /role/1
	 */
	@PostMapping("/roles/remove/{roleId}")
	@ApiOperation(value = SysRoleConstant.DELETE_ROLE)
	public ResponseEntity removeRoleByRoleId(@PathVariable Long roleId) {
		Try.of(() -> sysRoleService.deleteRole(roleId)).onFailure(ex -> log.error("role-deleteRole-error", ex))
				.getOrElseThrow(item -> new SysRoleException(SysRoleRtnCodEnum.SYS_ROLE_00003));
		return ResponseEntity.succeed(SysRoleRtnCodEnum.SYS_ROLE_00004.getRtnMessage());

	}

	/**
	 * 获取用户的角色
	 */
	@GetMapping(value = "/roles/userRoles", params = "userId")
	@ApiOperation(value = "根据id查询用户角色")
	public ResponseEntity<List<SysRole>> findRolesByUserId(@RequestParam Long userId) {
		return  ResponseEntity.succeed(sysUserRoleService.findRolesByUserId(userId));
	}
}
